package com.simin.base.appnet.netother;

import android.content.Context;

import com.simin.base.appnet.progress.ProgressCancelListener;
import com.simin.base.appnet.progress.ProgressDialogHandler;
import com.simin.base.appnet.progress.TokenListener;

import rx.Subscriber;

/**
 * 用于在Http请求开始时，自动显示一个ProgressDialog
 * 在Http请求结束是，关闭ProgressDialog
 * 调用者自己对请求数据进行处理
 * Created by wsm on 17/10/28.
 */
public class RxSubscriber<T> extends Subscriber<T> implements ProgressCancelListener {

    private OnNextListener mOnNextListener;
    private ProgressDialogHandler mProgressDialogHandler;

    private Context context;
    private boolean showDialog = true;
    private String showText = "加载中……";

    private TokenListener tokenListener;

    protected RxSubscriber() {

    }

    //默认dialog  显示的构造器
    public RxSubscriber(Context context, OnNextListener mOnNextListener) {
        this.mOnNextListener = mOnNextListener;
        this.context = context;
        mProgressDialogHandler = new ProgressDialogHandler(showText, context, this, false);
        this.tokenListener = (TokenListener) context;
    }

    //默认dialog  显示的构造器
    public RxSubscriber(String showText, Context context, OnNextListener mOnNextListener) {
        this.mOnNextListener = mOnNextListener;
        this.context = context;
        mProgressDialogHandler = new ProgressDialogHandler(showText, context, this, false);
        this.tokenListener = (TokenListener) context;
    }

    //修改dialog点击消失的  构造器
    public RxSubscriber(Context context, Boolean cliskIsDiss, OnNextListener mOnNextListener) {
        this.mOnNextListener = mOnNextListener;
        this.context = context;
        mProgressDialogHandler = new ProgressDialogHandler(showText, context, this, cliskIsDiss);
        this.tokenListener = (TokenListener) context;

    }

    //自定义显示文本，修改dialog点击消失的  构造器
    public RxSubscriber(Context context, String showText, Boolean cliskIsDiss, OnNextListener mOnNextListener) {
        this.mOnNextListener = mOnNextListener;
        this.context = context;
        mProgressDialogHandler = new ProgressDialogHandler(showText, context, this, cliskIsDiss);
        this.tokenListener = (TokenListener) context;

    }

    //修改改dialog  不显示的构造器
    public RxSubscriber(Boolean isShowDialog, Context context, OnNextListener mOnNextListener) {
        this.mOnNextListener = mOnNextListener;
        this.context = context;
        this.showDialog = isShowDialog;
        mProgressDialogHandler = new ProgressDialogHandler(showText, context, this, false);
        this.tokenListener = (TokenListener) context;
    }


    private void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 订阅开始时调用
     * 显示ProgressDialog
     */
    @Override
    public void onStart() {
        if (isShowDialog()) {
            showProgressDialog();
        }
    }

    /**
     * 完成，隐藏ProgressDialog
     */
    @Override
    public void onCompleted() {
        if (isShowDialog()) {
            dismissProgressDialog();
        }
    }

    /**
     * 对错误进行统一处理
     * 隐藏ProgressDialog
     *
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        if (isShowDialog()) {
            dismissProgressDialog();
        }
        if (mOnNextListener != null) {
            ApiException apiException = CustomException.handleException(e);
            if (tokenListener != null) {
                tokenListener.onFailure(apiException.code + "", apiException.msg);
            }
            mOnNextListener.onError(context, apiException.code, apiException.msg);
        }
    }

    /**
     * 将onNext方法中的返回结果交给Activity或Fragment自己处理
     *
     * @param t 创建Subscriber时的泛型类型
     */
    @Override
    public void onNext(T t) {
        if (tokenListener != null) {
            tokenListener.onSuccess(t);
        }
        if (mOnNextListener != null) {
            mOnNextListener.onNext(t);
        }
    }

    /**
     * 取消ProgressDialog的时候，取消对observable的订阅，同时也取消了http请求
     */
    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }


    public boolean isShowDialog() {
        return showDialog;
    }

}